package cn.iocoder.yudao.module.ems.controller.admin.warehousing;

import cn.iocoder.yudao.module.ems.convert.warehousing.WarehousingInfoConvert;
import cn.iocoder.yudao.module.ems.dal.dataobject.warehousing.WarehousingInfoDO;
import cn.iocoder.yudao.module.ems.service.warehousing.WarehousingInfoService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ems.controller.admin.warehousing.vo.*;
import cn.iocoder.yudao.module.ems.dal.dataobject.warehousing.WarehousingDO;
import cn.iocoder.yudao.module.ems.convert.warehousing.WarehousingConvert;
import cn.iocoder.yudao.module.ems.service.warehousing.WarehousingService;

@Api(tags = "设备管理 - 入库记录")
@RestController
@RequestMapping("/ems/warehousing")
@Validated
public class WarehousingController {

    @Resource
    private WarehousingService warehousingService;

    @Resource
    private WarehousingInfoService warehousingInfoService;

    @PostMapping("/create")
    @ApiOperation("创建入库记录")
    @PreAuthorize("@ss.hasPermission('ems:warehousing:create')")
    public CommonResult<Long> createWarehousing(@Valid @RequestBody WarehousingCreateReqVO createReqVO) {
        return success(warehousingService.createWarehousing(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新入库记录")
    @PreAuthorize("@ss.hasPermission('ems:warehousing:update')")
    public CommonResult<Boolean> updateWarehousing(@Valid @RequestBody WarehousingUpdateReqVO updateReqVO) {
        warehousingService.updateWarehousing(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除入库记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:warehousing:delete')")
    public CommonResult<Boolean> deleteWarehousing(@RequestParam("id") Long id) {
        warehousingService.deleteWarehousing(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得入库记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:warehousing:query')")
    public CommonResult<WarehousingRespVO> getWarehousing(@RequestParam("id") Long id) {
        WarehousingDO warehousing = warehousingService.getWarehousing(id);
        List<WarehousingInfoDO> warehousingInfoList = warehousingInfoService.getWarehousingInfoList(id);
        WarehousingRespVO resp = new WarehousingRespVO();
        resp.setId(warehousing.getId());
        resp.setTime(warehousing.getTime());
        resp.setNumber(warehousing.getNumber());
        resp.setCreateTime(warehousing.getCreateTime());
        resp.setRemarks(warehousing.getRemarks());
        resp.setReturnee(warehousing.getReturnee());
        resp.setStatus(warehousing.getStatus());
        resp.setType(warehousing.getType());
        resp.setWarehouser(warehousing.getWarehouser());
        resp.setWarehousingInfo(WarehousingInfoConvert.INSTANCE.convert(warehousingInfoList));
        return success(resp);
    }

    @GetMapping("/list")
    @ApiOperation("获得入库记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('ems:warehousing:query')")
    public CommonResult<List<WarehousingRespVO>> getWarehousingList(@RequestParam("ids") Collection<Long> ids) {
        List<WarehousingDO> list = warehousingService.getWarehousingList(ids);
        return success(WarehousingConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得入库记录分页")
    @PreAuthorize("@ss.hasPermission('ems:warehousing:query')")
    public CommonResult<PageResult<WarehousingRespVO>> getWarehousingPage(@Valid WarehousingPageReqVO pageVO) {
        PageResult<WarehousingDO> pageResult = warehousingService.getWarehousingPage(pageVO);
        return success(WarehousingConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出入库记录 Excel")
    @PreAuthorize("@ss.hasPermission('ems:warehousing:export')")
    @OperateLog(type = EXPORT)
    public void exportWarehousingExcel(@Valid WarehousingExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WarehousingDO> list = warehousingService.getWarehousingList(exportReqVO);
        // 导出 Excel
        List<WarehousingExcelVO> datas = WarehousingConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "入库记录.xls", "数据", WarehousingExcelVO.class, datas);
    }

    @DeleteMapping("/deleteInfo")
    @ApiOperation("删除入库信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:warehousing:delete')")
    public CommonResult<Boolean> deleteWarehousingInfo(@RequestParam("id") Long id) {
        warehousingInfoService.deleteWarehousingInfo(id);
        return success(true);
    }

    @PostMapping("/insertBatchInfo")
    @ApiOperation("批量创建入库信息")
    @PreAuthorize("@ss.hasPermission('ems:warehousing:create')")
    public CommonResult<Boolean> insertBatchInfo(@Valid @RequestBody Collection<WarehousingInfoCreateReqVO> infoList) {
        warehousingInfoService.insertBatchInfo(infoList);
        return success(true);
    }

    @PutMapping("/updateInfo")
    @ApiOperation("更新入库信息")
    @PreAuthorize("@ss.hasPermission('ems:warehousing:update')")
    public CommonResult<Boolean> updateWarehousingInfo(@Valid @RequestBody WarehousingInfoRespVO updateReqVO) {
        warehousingInfoService.updateWarehousingInfo(updateReqVO);
        return success(true);
    }

}
